package org.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppResult<T>{
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;
    public AppResult() {}
        public  AppResult(long code, String message) {
        this.code = (int) code;
        this.message = message;
        }
        public AppResult(long code,String message,T dat) {
        this.code = (int) code;
        this.message = message;
        this.data = dat;
        }

    public AppResult(String message) {
        this.message = message;
    }

    /**
     * 成功
     * @param data 返回结果

     */
        public static <T> AppResult<T> success(T data) {
        return new AppResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
        }

    /**
     *
     * @param message
     * @param data

     */
        public static <T> AppResult<T> success(String message,T data) {
            return new AppResult<T>(ResultCode.SUCCESS.getCode(), message, data);

        }
        public static <T> AppResult<T> success() {
            return new AppResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
        }

    /**
     *失败
     * @param message
     * @return
     * @param <T>
     */
        public static <T> AppResult<T> failed(String message) {
            return new AppResult<T>(message);
        }
        public static AppResult failed(ResultCode resultCode) {
            return new AppResult(resultCode.getCode(), resultCode.getMessage());
        }
        public static <T> AppResult<T> failed() {
            return new AppResult<T>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
        }
        public static <T> AppResult<T> failed(int code, String message) {
            return new AppResult<T>(code, message);
        }
        public static <T> AppResult<T> failed( T dat) {
        return new AppResult<T>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), dat);
        }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
